<?php

require_once 'az-cart-ajax.php';

define('THEME_URL', get_template_directory_uri());

/*// Fillter Search
function searchfilter($query) {

    if ($query->is_search && !is_admin() ) {
        $query->set('post_type',array('seafood'));
    }
    return $query;
}
add_filter('pre_get_posts','searchfilter');*/

function __search_by_title_only( $search, &$wp_query )
{
    global $wpdb;
    if ( empty( $search ) )
        return $search; // skip processing - no search term in query
    $q = $wp_query->query_vars;
    $n = ! empty( $q['exact'] ) ? '' : '%';
    $search =
    $searchand = '';
    foreach ( (array) $q['search_terms'] as $term ) {
        $term = esc_sql( like_escape( $term ) );
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
    }
    if ( ! empty( $search ) ) {
        $search = " AND ({$search}) ";
        if ( ! is_user_logged_in() )
            $search .= " AND ($wpdb->posts.post_password = '') ";
    }
    return $search;
}
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );


// Add image size
function azio_theme_setup() {
    add_image_size( 'avatar', 100, 100, true ); // 1x1
    add_image_size( 'featured-image',400, 161, true ); // Tin chinh
    add_theme_support( 'post-thumbnails' ); 
}
add_action( 'after_setup_theme', 'azio_theme_setup' );

// Excerpt length
function wpe_excerptlength_cat($length) {
    return 80;
};
function wpe_excerptlength_sidebar($length) {
    return 15;
};
function wpe_excerptlength_index($length) {
    return 45;
};

function wpe_excerptmore($more) {
    return '...';
};

function wpe_excerpt($length_callback='', $more_callback='') {
    global $post;
    if(function_exists($length_callback)){
        add_filter('excerpt_length', $length_callback);
    }
    if(function_exists($more_callback)){
        add_filter('excerpt_more', $more_callback);
    }
    $output = get_the_excerpt();
    $output = apply_filters('wptexturize', $output);
    $output = apply_filters('convert_chars', $output);
    echo $output;
};

// function az_modify_num_posts_for_seafood($query)
// {
    // if ($query->is_main_query() && $query->is_post_type_archive('seafood') && !is_admin())
        // $query->set('posts_per_page', 9);
// }
// add_action('pre_get_posts', 'az_modify_num_posts_for_seafood');





function azio_scripts_styles(){
    $version = '19102013_0830';
    
    // enqueue style
    wp_enqueue_style( 'az-reset', THEME_URL . '/css/reset.css', array(), $version, 'screen' );
    wp_enqueue_style( 'az-text', THEME_URL .'/css/style.css', array('az-reset'), $version, 'screen');
    wp_enqueue_style('az-style-theme', THEME_URL . '/style.css', array('az-reset'), $version);

    wp_enqueue_style( 'az-slider-css', THEME_URL .'/css/nivo-slider2.css', array('az-reset'), $version, 'screen');
    wp_enqueue_style( 'az-sagscroller', THEME_URL .'/css/sagscroller.css', array('az-reset'), $version, 'screen');

    // enqueue script
    wp_enqueue_script( 'az-slider-js', THEME_URL . '/js/jquery.nivo.slider.pack1.js', array('jquery'), $version, true );

    wp_enqueue_script( 'az-jcarousellite', THEME_URL . '/js/jcarousellite_1.0.1c5.js', array('jquery'), $version, true );
    wp_enqueue_script( 'sagscroller.js', THEME_URL . '/js/sagscroller.js', array('jquery'), $version, true );
    wp_enqueue_script( 'az-common', THEME_URL . '/js/common.js', array('jquery'), $version, true );

    

//    // Load JS Ajax
//    wp_enqueue_script('azio-ajax-handle', THEME_URL . '/js/az-cart-ajax.js', array('jquery'), $version, true);
//    wp_localize_script('azio-ajax-handle', 'azio_ajax', array('ajaxurl' => admin_url('admin-ajax.php')));

}
add_action( 'wp_enqueue_scripts', 'azio_scripts_styles' );

